// pages/my/my.js
import cache from "../../enum/cache"
import Token from "../../model/token";
import { appointWithMeGrid, myAppointGrid, myProvideGird, mySeekGrid } from "../../config/grid"
import Order from "../../model/order";
import roleType from "../../enum/role-type";
import Service from "../../model/service";
import serviceType from "../../enum/service-type";
Page({

    /**
     * 页面的初始数据
     */
    data: { 
        count: 10,
        userInfo : {
            avatar : '../../assets/images/logo.png',
            nickname : '点击授权登录'
        },
        appointWithMeGrid,
        myAppointGrid,
        myProvideGird,
        mySeekGrid,

        appointWithMeStatus: null,
        myAppointStatus: null,
        provideServiceStatus: null,
        seekServiceStatus: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },

    handleToLogin : function (){
        wx.navigateTo({
            url : '/pages/login/login'
        })
    },
    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: async function () {
        // 验证token是否过期
        const verifyToken = await Token.verifyToken()
        // console.log(verifyToken);
        if(verifyToken.valid){
            const userInfo = wx.getStorageSync(cache.USERINFO);
            // console.log(userInfo);
            this.setData({
                ['userInfo.avatar']: userInfo.avatar,
                ['userInfo.nickname']: userInfo.nickname
            })


            this._getOrderStatus();
            this._getServiceStatus();
        }
        
    },

    // 获取订单的统计数量
    async _getOrderStatus(){
        // 发布者
        const appointWithMeStatus = Order.getOrderStatus(roleType.PUBLISHER)
        // 消费者
        const myAppointStatus = Order.getOrderStatus(roleType.CONSUMER)
        // console.log('发布者' , appointWithMeStatus);
        // console.log('消费者' , myAppointStatus);
        this.setData({
            appointWithMeStatus: await appointWithMeStatus,
            myAppointStatus: await myAppointStatus
        })
    },


    // 获取服务的统计数量
    async _getServiceStatus(){
        // 在提供
        const provideServiceStatus = Service.getServiceStatus(serviceType.PROVIDE)
        // 正在找
        const seekServiceStatus = Service.getServiceStatus(serviceType.SEEK)
        this.setData({
            provideServiceStatus: await provideServiceStatus,
            seekServiceStatus: await seekServiceStatus
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})